<?php
/*
 * @project CreativeFramework
 * @version <#RELEASE_VERSION#>
 * @license GNU Lesser General Public License
 * @author Jimmie Lin
 */

function nice_dump($v){ // Just a wrapper...
	echo "<xmp>";
	echo print_r($v, true);
	echo "</xmp>";
}

function die_error($content){ // Nicer die with CSS Effects.
	echo "<style type='text/css'>body{font-family:Arial;font-size:12px;}.error{background:#f7f7f7;border:1px solid #ccc;padding: 5px;margin:3px;}table{border: 1px solid #CCC;margin-top: 5px}th{background:#424242;color:#fff}";
	echo "</style>";
	die($content);
}

function throwFatalError($message){ // Die.
	if(IN_DEV){
		$stack = "<hr /><br /><table width='95%' align='center'>";
		$sOut = ""; $aCallstack = debug_backtrace();
		$stack .= "<thead><tr><th>file</th><th>line</th><th>function</th>".
			"</tr></thead>";
		foreach($aCallstack as $aCall)
		{
			if (!isset($aCall['file'])) $aCall['file'] = '[PHP Kernel]';
			if (!isset($aCall['line'])) $aCall['line'] = 'Unknown';
			$stack .= "<tr><td>{$aCall["file"]}</td><td> {$aCall["line"]}</td>".
				"<td>{$aCall["function"]}</td></tr>";
		}
		$stack .= "</table>";
		die_error("<div class='error'><b>CF4 Triggered Error:</b> {$message}</div>
		<b>Debug Traceback:</b>".$stack."<small><center>
		Creative Framework G4 &copy; 2009
		</center></small>");
	}
	else{}
}

function handle_php_error($level, $message, $file = false, $line = false, $context = false){
	switch($level){
		case E_USER_ERROR: $level = "User Error"; break;
		case E_USER_WARNING: $level = "User Warning"; break;
		case E_WARNING: $level = "Warning"; break;
		case E_USER_NOTICE: $level = "User Notice"; break;
		case E_NOTICE: $level = "Notice"; break;
		default: $level = "UNKNOWN/$level";
	}
	
	if(IN_DEV){
		$stack = "<br /><table width='95%' align='center'>";
		$sOut=""; $aCallstack=debug_backtrace();
	   
		$stack .= "<thead><tr><th>file</th><th>line</th><th>function</th>".
			"</tr></thead>";
		foreach($aCallstack as $aCall)
		{
			if (!isset($aCall['file'])) $aCall['file'] = '[PHP Kernel]';
			if (!isset($aCall['line'])) $aCall['line'] = 'Unknown';
			$stack .= "<tr><td>{$aCall["file"]}</td><td> {$aCall["line"]}</td>".
				"<td>{$aCall["function"]}</td></tr>";
		}
		$stack .= "</table>";
		die_error("<div class='error'><b>{$level}:</b> {$message} on line {$line} of file {$file}</div>
		".$stack."<small><center>
		Creative Framework G4 &copy; 2009
		</center></small>");
	}
	else{}
	return true;
}

set_error_handler("handle_php_error");
